package com.linghui.videoplus.ipai.activity;

import java.io.Serializable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.UnderlineSpan;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.linghui.videoplus.ipai.R;
import com.linghui.videoplus.ipai.VideoPlusApplication;
import com.linghui.videoplus.ipai.api.Preferences;
import com.linghui.videoplus.ipai.api.json.JsonAPI;
import com.linghui.videoplus.ipai.bean.VideoBean;
import com.linghui.videoplus.ipai.service.UploadService;
import com.linghui.videoplus.ipai.utils.Constants;
import com.linghui.videoplus.ipai.utils.StringUtil;

public class UploadActivity extends BaseActivity implements OnTouchListener {

	private EditText uplaodTitleEditText;
	private EditText uplaodDescEditText;
	private TextView uplaodTextView;

	private VideoBean video;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_upload);
		video = (VideoBean) getIntent().getSerializableExtra(SERIALIZABLE__KEY);
		JsonAPI jsonAPI = new JsonAPI();

		uplaodTitleEditText = (EditText) findViewById(R.id.uplaodTitleEditText);
		uplaodTitleEditText.setText(jsonAPI.getNickName() + "_" + video.getId());

		uplaodDescEditText = (EditText) findViewById(R.id.uplaodDescEditText);
		uplaodDescEditText.setText(video.getDesc());

		uplaodTextView = (TextView) findViewById(R.id.uploadTextView);
		
		SpannableString msp = new SpannableString("开始上传");
		msp.setSpan(new UnderlineSpan(), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
		uplaodTextView.setText(msp);
		uplaodTextView.setOnTouchListener(this);

		

	}
	
	

	public static void launch(Activity mActivity, int fromCode, int toCode,
			Serializable obj) {
		Intent intent = new Intent(mActivity, UploadActivity.class);
		intent.putExtra(FROM_CODE_KEY, fromCode);
		intent.putExtra(TO_CODE_KEY, toCode);
		intent.putExtra(SERIALIZABLE__KEY, obj);
		mActivity.startActivity(intent);
		mActivity.finish();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(v==uplaodTextView){
			if (event.getAction() == MotionEvent.ACTION_UP) {
				String title = uplaodTitleEditText.getText().toString();
				String desc = uplaodDescEditText.getText().toString();
				if(checkUploadInfo(title, desc)){
					if(!VideoPlusApplication.checkNetWorkStatus(this)){
						showToast(this.getResources().getString(R.string.network_error_two));
						return true;
					}
					
					if(VideoPlusApplication.isLogin(this)){
						String uid=Preferences.getLocalUid(this);
						JsonAPI jsonApi=new JsonAPI();
						String author=jsonApi.getNickName();
						Intent intent = new Intent(this, UploadService.class);
						intent.putExtra(Constants.EXTRA_UPLOAD_UID_KEY, uid);
						intent.putExtra(Constants.EXTRA_UPLOAD_AUTHOR_KEY, author);
						intent.putExtra(Constants.EXTRA_UPLOAD_TITLE_KEY, title);
						intent.putExtra(Constants.EXTRA_UPLOAD_DESC_KEY, desc);
						intent.putExtra(Constants.EXTRA_UPLOAD_ENTRY, video);
						startService(intent);
						MyVideoActivity.launch(this, REQUEST_CODE_UPLOAD, REQUEST_CODE_MY_VIDEO, video);
					}else{
						super.showToast(getResources().getString(R.string.hint_no_login));
						AccountActivity.launch(this, REQUEST_CODE_UPLOAD, REQUEST_CODE_ACCOUNT,video);
					}
				}
			}
			return true;
		}
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			Intent intent = new Intent(this, MyVideoActivity.class);
			intent.putExtra(FROM_CODE_KEY, REQUEST_CODE_UPLOAD);
			intent.putExtra(TO_CODE_KEY, REQUEST_CODE_MY_VIDEO);
			intent.putExtra(SERIALIZABLE__KEY, video);
			startActivity(intent);
			overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
			this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 验证登录字段
	 * 
	 * @param name
	 * @param pass
	 * @return
	 */
	private boolean checkUploadInfo(final String title,final String desc) {
		if (StringUtil.isEmpty(title)) {
			showToast("请输入标题!");
			return false;
		}
		if(title.length()>28){
			showToast("标题仅支持28个字符!");
			return false;
		}
		
		if (StringUtil.isEmpty(desc)) {
			showToast("请输入描述!");
			return false;
		}
		if(desc.length()>60){
			showToast("描述仅支持60个字符!");
			return false;
		}

		return true;
	}
	
	
	
}
